package com.nit.lab.dao.Impl;

import com.nit.lab.dao.LoginDao;
import com.nit.lab.dto.DtoLoginUser;
import com.nit.lab.model.SysUserEntity;
import com.nit.lab.utils.SystemConstants;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by CYT on 2017/11/19.
 */
@Service
public class LoginDaolmpl implements LoginDao{
    @Resource
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Override
    public boolean findUserName(String name) throws Exception {
        String hql = "from SysUserEntity where userName =:name and isDelete = 0";
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setString("name",name);
        if (query.list().size()==0){
            //list为空，不存在该用户名。
            return false;
        }
        else {
            return true;
        }
    }

    @Override
    public boolean findUserPhone(String phone) throws Exception {
        String hql = "from SysUserEntity where userPhone =:phone and isDelete = 0";
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setString("phone",phone);
        if (query.list().size()==0){
            //list为空，不存在该用户名。
            return false;
        }
        else {
            return true;
        }
    }

    @Override
    public boolean findUserEmail(String email) throws Exception {
        String hql = "from SysUserEntity where userEmail =:email and isDelete = 0";
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setString("email",email);
        if (query.list().size()==0){
            //list为空，不存在该用户名。
            return false;
        }
        else {
            return true;
        }
    }

    @Override
    public List<DtoLoginUser> selectUser(String userName, String userCode) throws Exception {
        List<DtoLoginUser> result = null;
        String hql = "select new com.nit.lab.dto.DtoLoginUser(userId, userName, userRealName, userPhone, userCode, actorId) from SysUserEntity where userName  =:userName and userCode =:userCode and isDelete = 0";
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setString("userName",userName);
        query.setString("userCode",userCode);
        //setString()方法包含两个参数：命名参数名称，命名参数实际值。
        result = query.list();
        return result;
    }

    @Override
    public boolean changCode(String userEmail, String userPhone) throws Exception {
        String hql = "from SysUserEntity where userEmail  =:userEmail and userPhone =:userPhone and isDelete = 0";
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setString("userEmail",userEmail);
        query.setString("userPhone",userPhone);
        if (query.list().size()==0){
            //list为空，不存在该用户名。
            return false;
        }
        else {
            return true;
        }
    }

    @Override
    public Integer insertUserInfo(SysUserEntity sysUserEntity) throws Exception {
        Integer result = SystemConstants.Insert_Default;
        //Insert_Default = -1
        Session session = this.getSessionFactory().getCurrentSession();
        result = (Integer) session.save(sysUserEntity);
        return result;
    }


}
